"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MySqlUtil = void 0;
const mysql = require("mysql2");
const Logger_1 = require("./Logger");
const fs = require("fs");
const path = require("path");
class MySqlUtil {
    static initConfig(configPath = path.resolve() + "/config/mysql.json") {
        let data = fs.readFileSync(configPath);
        let obj = JSON.parse(data.toString());
        this.MySql_Host = obj["host"];
        this.MySql_Port = obj["port"];
        this.MySql_User = obj["user"];
        this.MySql_Pass = obj["pass"];
    }
    static init() {
        return __awaiter(this, void 0, void 0, function* () {
            this.initConfig();
            let config = {
                "host": this.MySql_Host,
                "port": this.MySql_Port,
                "user": this.MySql_User,
                "password": this.MySql_Pass,
            };
            this.connection = mysql.createConnection(config);
            Logger_1.Logger.log("MySqlUtil.init", config, this.connection);
            this.connection.on("connect", (err) => {
                if (!err) {
                    this.isConnected = true;
                    Logger_1.Logger.log("MySqlUtil.onConnect==", err);
                }
                else {
                    Logger_1.Logger.log("MySqlUtil.onConnect=error=", err);
                }
            });
            this.connection.on("error", (err) => {
                this.isConnected = false;
                Logger_1.Logger.log("MySqlUtil.onError==", err);
            });
            this.connection.on("end", (err) => {
                // this.isConnected = false;
                Logger_1.Logger.log("MySqlUtil.end==", err);
            });
            yield this.connect();
            // this.connection.end();
            this.connection.query("show database;", (err, result) => {
                if (err) {
                    Logger_1.Logger.error('[database eror] - ', err.message);
                    return;
                }
                Logger_1.Logger.log("database resp=", result);
            });
            // let test:string = "SELECT * FROM users;";
            // let test:string = "INSERT INTO users values(null, 100003, 'test3', 'nickname3');";
            // this.executeSql(test);
        });
    }
    static connect() {
        return new Promise((resolve, reject) => {
            if (!this.isConnected) {
                this.connection.connect((err) => {
                    if (err) {
                        Logger_1.Logger.error('MySqlUtil connect error:', err);
                        return;
                    }
                    Logger_1.Logger.log('[MySqlUtil connect]  succeed!');
                    resolve(null);
                });
            }
            else {
                resolve(null);
            }
        });
    }
    static select(sql, callback = null) {
        // let  sql = 'SELECT * FROM websites';
        //查
        this.init();
        this.connection.query(sql, (err, result) => {
            if (err) {
                Logger_1.Logger.error('[SELECT ERROR] - ', err.message);
                if (callback) {
                    callback(err);
                }
                return;
            }
            // Logger.log("select resp=", result);
            if (callback) {
                callback(err, result);
            }
        });
    }
    static insert(sql, addSqlParams, callback = null) {
        // var  addSql = 'INSERT INTO websites(Id,name,url,alexa,country) VALUES(0,?,?,?,?)';
        // var  addSqlParams = ['菜鸟工具', 'https://c.runoob.com','23453', 'CN'];
        // REPLACE INTO table (id, name, user, passwd) VALUES (1, '极客点儿', 'admin', 'abcdef');
        this.init();
        //增
        this.connection.query(sql, addSqlParams, (err, result) => {
            if (err) {
                Logger_1.Logger.error('[INSERT ERROR] - ', err.message);
                if (callback) {
                    callback(err);
                }
                return;
            }
            Logger_1.Logger.log("INSERT resp=", result);
            if (callback) {
                callback(err, result);
            }
        });
    }
    static update(modSql, modSqlParams, callback = null) {
        // REPLACE INTO table (id, name, user, passwd) VALUES (1, '极客点儿', 'admin', 'abcdef');
        // var modSql = 'UPDATE websites SET name = ?,url = ? WHERE Id = ?';
        // var modSqlParams = ['菜鸟移动站', 'https://m.runoob.com', 6];
        this.init();
        //改
        this.connection.query(modSql, modSqlParams, (err, result) => {
            if (err) {
                Logger_1.Logger.error('[UPDATE ERROR] - ', err.message);
                if (callback) {
                    callback(err);
                }
                return;
            }
            Logger_1.Logger.log("UPDATE resp=", result);
            if (callback) {
                callback(err, result);
            }
        });
    }
    static delete(delSql, callback) {
        // var delSql = 'DELETE FROM websites where id=6';
        this.init();
        //删
        this.connection.query(delSql, (err, result) => {
            if (err) {
                Logger_1.Logger.error('[DELETE ERROR] - ', err.message);
                if (callback) {
                    callback(err);
                }
                return;
            }
            Logger_1.Logger.log("DELETE resp=", result);
            if (callback) {
                callback(err, result);
            }
        });
    }
}
exports.MySqlUtil = MySqlUtil;
MySqlUtil.MySql_Host = ""; //
MySqlUtil.MySql_Port = 3306; //
MySqlUtil.MySql_User = "";
MySqlUtil.MySql_Pass = "";
